//
//  SchoolViewCell.m
//  Hxcsfw
//
//  Created by 企友软mac on 2017/10/27.
//  Copyright © 2017年 企友软mac. All rights reserved.
//

#import "SchoolHeadViewCell.h"
#import "iCarouselModel.h"
#import "MJExtension.h"
#import "HotLbtModel.h"
#import "UIImageView+WebCache.h"

@implementation SchoolHeadViewCell

- (void)awakeFromNib {
    [super awakeFromNib];

    _arrTitle = [NSMutableArray array];
    [_arrTitle removeAllObjects];

    _arrLink = [NSMutableArray array];
    [_arrLink removeAllObjects];

    for (NSDictionary *dic in [WShareUtil sharedData].arrShOfTit){
        NSString *str = [dic[@"title"] substringToIndex:1];
        if ([str isEqualToString:@"【"]) {
            [_arrTitle addObject:dic[@"title"]];
        } else {
            [_arrTitle addObject:[NSString stringWithFormat:@"   %@",dic[@"title"]]];
        }
        [_arrLink addObject:dic[@"link"]];
    }

    CCPScrollView *ccpView = [[CCPScrollView alloc] initWithFrame:CGRectMake(-10, 0, WSCREENH_WIDTH - 140, _lbScrollView.height)];

    if (iPhone6Plus_6sPlus) {
        _spaceCon.constant = 20;
    } else {
        _spaceCon.constant = 8;
    }
    if (_arrTitle.count > 0) {
        ccpView.titleArray = [_arrTitle copy];
    }

    ccpView.titleFont = 13;

    ccpView.titleColor = [UIColor blackColor];

//    ccpView.BGColor = WRGBColor(255, 255, 255);

    [_lbScrollView addSubview:ccpView];

    [ccpView clickTitleLabel:^(NSInteger index,NSString *titleString) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegatScrollClick:)]){
            [self.delegate delegatScrollClick:index];
        }
    }];

    _arrayOfAds = [NSMutableArray array];
    [_arrayOfAds removeAllObjects];

    for (NSDictionary *dic in [WShareUtil sharedData].arrShOfAds){
        HotLbtModel *model= [HotLbtModel mj_objectWithKeyValues:dic];
        [_arrayOfAds addObject:model];
    }

    _iCarouselView.type = iCarouselTypeCylinder;
    _iCarouselView.decelerationRate =0.4;
    _iCarouselView.scrollSpeed =0.4;
    _iCarouselView.delegate = self;
    _iCarouselView.dataSource = self;

    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.iCarouselView.frame.size.height-20, self.iCarouselView.frame.size.width, 20)];

    pageControl.numberOfPages = _arrayOfAds.count;

    CGSize pointSize = [pageControl sizeForNumberOfPages:_arrayOfAds.count];

    CGFloat page_x = -(pageControl.bounds.size.width - pointSize.width) / 2 ;

    [pageControl setBounds:CGRectMake(page_x + 15, pageControl.bounds.origin.y,
                                      pageControl.bounds.size.width, pageControl.bounds.size.height)];
    pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
    pageControl.pageIndicatorTintColor = [UIColor brownColor];
    pageControl.userInteractionEnabled = NO;

    self.pageControl = pageControl;

    [self.iCarouselView addSubview:self.pageControl];

    [self addTimer];

}

-(void)addTimer{
    self.timer=[NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(nextCarousel) userInfo:nil repeats:YES];
}

//自动播放下一条广告功能
-(void)nextCarousel{
    NSInteger page=0;
    if (self.pageControl.currentPage == self.pageControl.numberOfPages-1) {
        page=0;
    }else{
        page=self.pageControl.currentPage+1;
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3f];

    self.pageControl.currentPage=page;
    //设置icarouselAdView
    self.iCarouselView.currentItemIndex=page;
    [UIView commitAnimations];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc {
    if (self.timer) {
        [self.timer invalidate];
        self.timer=nil;
    }
}

#pragma mark -iCarouselDataSource&&iCarouselDelegate
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel{
    
    return [_arrayOfAds count];
}

- (NSInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel{

    return 0;
}

- (CGFloat)carouselItemWidth:(iCarousel *)carousel{

    return carousel.frame.size.width;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{

    UIImageView *imageView;

    if (carousel == _iCarouselView) {
        if(view == nil){
            view=[[UIView alloc]initWithFrame:self.iCarouselView.bounds];
            imageView=[[UIImageView alloc]initWithFrame:view.bounds];
            [view addSubview:imageView];
        }
        else{
            imageView =[[view subviews] lastObject];
        }

        if ([_arrayOfAds[index] isKindOfClass:[HotLbtModel class]]) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:[_arrayOfAds[index] imgsrc]] placeholderImage:[UIImage imageNamed:@"placeholderImage.png"]];
        }else {
            imageView.image = [UIImage imageNamed:@"placeholderImage.png"];
        }
    }
    return view;
}

- (IBAction)btnClick:(UIButton *)sender {

    if (self.delegate && [self.delegate respondsToSelector:@selector(delegatBtnClick:)]){
        [self.delegate delegatBtnClick:sender];
    }
}

//点击事件
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(delegatiCarClick:)]){
        [self.delegate delegatiCarClick:index];
    }
}

-(void)carouselDidScroll:(iCarousel *)carousel{
    self.pageControl.currentPage = carousel.currentItemIndex;
}

- (void)carouselWillBeginDragging:(iCarousel *)carousel{
    [self.timer invalidate];
    self.timer=nil;
}

- (void)carouselDidEndDragging:(iCarousel *)carousel willDecelerate:(BOOL)decelerate{
    [self addTimer];
}

@end
